//
//  GameUtil.cpp
//  DiceWar
//
//  Created by Xiao  Feng on 17/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "GameUtil.h"

namespace GameUtil 
{
    cocos2d::CCSize getWindowSize()
    {
        return cocos2d::CCDirector::sharedDirector()->getWinSize();
    }
    
    cocos2d::CCPoint getPointFromLocationInView(const cocos2d::CCPoint& location)
    {
        cocos2d::CCPoint point;
        point.x = location.x;
        
        const cocos2d::CCSize kWinSize = getWindowSize();
        point.y = kWinSize.height - location.y;
        
        return point;
    }
    
    void setRelativeToScreenPosition(cocos2d::CCNode* pNode, const cocos2d::CCPoint& relativePosition)
    {
        if (!pNode) return;
        
        const cocos2d::CCSize kWinSize = getWindowSize();
        const float realPositionX = kWinSize.width * relativePosition.x;
        const float realPositionY = kWinSize.height * relativePosition.y;
        pNode->setPosition(realPositionX, realPositionY);
    }
    
    void setRelativeToScreenSize(cocos2d::CCNode* pNode, const cocos2d::CCSize& relativeSize)
    {
        if (!pNode) return;
        
        const cocos2d::CCSize kWinSize = getWindowSize();
        const float kRealSizeX = kWinSize.width * relativeSize.width;
        const float kRealSizeY = kWinSize.height * relativeSize.height;
        
        const cocos2d::CCSize kNodeSize = pNode->getContentSize();
        const float kScaleX = kRealSizeX / kNodeSize.width;
        const float kScaleY = kRealSizeY / kNodeSize.height;
        pNode->setScaleX(kScaleX);
        pNode->setScaleY(kScaleY);
    }
}

